<?php

namespace App\Module;

use App\depository\PermissionsDep;
use App\depository\RoleHasPermissionsDep;
use App\depository\RolesDep;
use App\Enum\CommonEnum;
use App\Enum\PermissionsEnum;
use App\Enum\RolesEnum;
use App\Service\DictService;

class RolesModule extends BaseModule
{

    public function init($request)
    {

        $param = $request->input();

        $dep = new PermissionsDep();

        $permissionsTree = $this->permissionsTree();

        $dictService = new DictService();
        $dictService
            ->setRolesStatus();

        $data = [
            'dict' => $dictService->getDict(),
            'tree' => $permissionsTree
        ];

        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();

        if(empty($param['name'])){
            return self::response([],'请填写标题',100);
        }

        if(empty($param['guard_name'])){
            return self::response([],'请填写标识',100);
        }

        $dep = new RolesDep();
        $roleHasPermissionsDep = new RoleHasPermissionsDep();

        $res = $dep->firstByGuardName($param['guard_name']);
        if($res){
            return self::response([],'角色标识已存在，请重新输入',200);
        }

        $data = [
            'name' => $param['name'],
            'guard_name' => $param['guard_name'],
            'remark' => $param['remark'] ?? "",
            'status' => $param['status']
        ];

        $roleId = $dep->addGetId($data);
        if(!empty($param['permission_id'])){
            foreach ($param['permission_id'] as $permissionId){
                $data = [
                  'role_id' => $roleId,
                  'permission_id' => $permissionId
                ];
                $roleHasPermissionsDep->add($data);
            }
        }
        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new RolesDep();

        $roleHasPermissionsDep = new RoleHasPermissionsDep();
        $roleHasPermissionsDep->delByRoleId($param['id']);

        $dep->del($param['id']);
        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        if(empty($param['name'])){
            return self::response([],'请填写标题',100);
        }

        if(empty($param['guard_name'])){
            return self::response([],'请填写标识',100);
        }

        $data = [
            'name' => $param['name'],
            'guard_name' => $param['guard_name'],
            'remark' => $param['remark'] ?? "",
            'status' => $param['status']
        ];

        $dep = new RolesDep();
        $dep->upd($param['id'],$data);

        $roleHasPermissionsDep = new RoleHasPermissionsDep();
        $roleHasPermissionsDep->delByRoleId($param['id']);

        if(!empty($param['permission_id'])){
            foreach ($param['permission_id'] as $permissionId){
                $data = [
                    'role_id' => $param['id'],
                    'permission_id' => $permissionId
                ];
                $roleHasPermissionsDep->add($data);
            }
        }

        return self::response();

    }

    public function list($request)
    {
        $param = $request->input();

        $rolesDep = new RolesDep();
        $resRoles = $rolesDep->list();

        $roleHasPermissions = new RoleHasPermissionsDep();

        $data['list'] = $resRoles->map(function($item) use ($roleHasPermissions){
            $resPermissions = $roleHasPermissions->getByRoleId($item->id);
            return [
                'id' => $item->id,
                'parent_id' => $item->parent_id,
                'name' => $item->name,
                'guard_name' => $item->guard_name,
                'remark' => $item->remark,
                'status' => $item->status,
                'status_show' => RolesEnum::$statusArr[$item->status] ?? CommonEnum::DEFAULT_NULL,
                'permission_id' => $resPermissions->pluck('permission_id')
            ];
        });

        return self::response($data);
    }

    public function permissionsTree()
    {

        $permissionsDep = new PermissionsDep();
        $resAllPermissions = $permissionsDep->normal();

        $allPermissions = $resAllPermissions->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'remark' => $item->remark,
                'parent_id' => $item->parent_id,
                'type' => $item->type,
                'type_show' => PermissionsEnum::$typeArr[$item->type] ?? CommonEnum::DEFAULT_NULL,
                'icon' => $item->icon,
                'guard_name' => $item->guard_name,
                'status' => $item->status,
                'status_show' => PermissionsEnum::$statusArr[$item->status] ?? CommonEnum::DEFAULT_NULL
            ];
        });

        $tree = $this->listToTree($allPermissions->toArray());
        return $tree;
    }


    public function listToTree($list, $rootId = 0)
    {
        $arr = [];
        foreach ($list as $item) {
            if ($item['parent_id'] == $rootId) {
                $children = $this->listToTree($list, $item['id']);
                if ($children) {
                    $item['children'] = $children;
                }
                $arr[] = $item;
            }
        }
        return $arr;
    }
}

